Option Strict Off
Option Explicit On
Namespace BMLERP
	Friend Class frmProgramRight
		Inherits System.Windows.Forms.Form
		Dim OldRow As Short
		Dim PadcodeSelect As String
		Function DeleteParent(ByRef PadDelete As Object, ByRef DeleteType As Object) As Object
			Dim a As Object
			Dim RecGroupFunction, RecCount As Object
			Dim recChild As ADODB.Recordset
			Dim ParentCode As Object
			Dim FullPad As String
			Dim CountSameParent As Short
			RecGroupFunction = DBconn.Execute("SELECT " & " GroupFunctionTemp.Padcode, GroupFunctionTemp.Selected," & " MenuPad.ParentPadCode , MenuPad.FullPadName " & " FROM GroupFunctionTemp INNER JOIN    MenuPad ON    GroupFunctionTemp.Padcode = MenuPad.PadCode" & " WHERE somay=" & MAYSO & "  and GroupFunctionTemp.PadCode='" & PadDelete & "'")
			ParentCode = RecGroupFunction!ParentPadCode
			FullPad = RecGroupFunction!FullPadName
			If DeleteType = 0 Then
				'Delete all Pad  hold Fullpad string (all child pad code of Pad Delete)
				recChild = DBconn.Execute("SELECT " & " GroupFunctionTemp.Padcode, GroupFunctionTemp.Selected," & " MenuPad.ParentPadCode , MenuPad.FullPadName " & " FROM GroupFunctionTemp INNER JOIN    MenuPad ON    GroupFunctionTemp.Padcode = MenuPad.PadCode" & " WHERE somay=" & MAYSO & " and SELECTED=1 ORDER BY  FullPadName ")
				If Not recChild.EOF Then
					Do Until recChild.Fields("FullPadName").Value = FullPad
						recChild.MoveNext()
						If recChild.EOF Then Exit Do
					Loop
				End If
				Do While Not recChild.EOF
					If InStr(1, recChild.Fields("FullPadName").Value, FullPad, 0) > 0 Then
						DBconn.Execute("UPDate GroupFunctionTemp SET Selected=0 Where Padcode='" & recChild.Fields("Padcode").Value & "'")
					Else
						Exit Do
					End If
					recChild.MoveNext()
				Loop
			End If
			DBconn.Execute("UPDate GroupFunctionTemp SET Selected=0 Where Padcode='" & PadDelete & "'")
			If ParentCode = "" Then Exit Function
			RecCount = DBconn.Execute("SELECT MenuPad.ParentPadCode, COUNT(GroupFunctionTemp.PadCode) As CountPadcode " & " FROM GroupFunctionTemp INNER JOIN   MenuPad ON    GroupFunctionTemp.PadCode = MenuPad.PadCode " & " WHERE MenuPad.ParentPadCode='" & ParentCode & "' AND Selected=1" & " GROUP BY MenuPad.ParentPadCode")
			If RecCount.EOF Then
				CountSameParent = 0
			Else
				CountSameParent = RecCount!CountPadCode
			End If
			If CountSameParent > 0 Then
				Exit Function
			Else
				a = DeleteParent(ParentCode, 1)
			End If
		End Function

		Private Sub cboUserGroup_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboUserGroup.ClickEvent
			If eventArgs.area = 2 Then
				UpdateSelected()
				FrameCheck.Visible = False
				Adodc1.Refresh()
				'grdFunction.CtlRefresh()
				'grdFunction.DataSource = Adodc1
				grdFunction.Refresh()
				Dim myDataCaptions As String() = {"Selected", "Pad Name", "Pad code"}
				Dim myDataFields As String() = {"Selected", "Caption", "Padcode"}
				setDataGridView(myDataCaptions, myDataFields, Me.grdFunction)

			End If
		End Sub

		Private Sub chkSelect_MouseUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles chkSelect.MouseUp
			Dim Button As Short = eventArgs.Button \ &H100000
			Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys \ &H10000
			Dim X As Single = VB6.PixelsToTwipsX(eventArgs.X)
			Dim y As Single = VB6.PixelsToTwipsY(eventArgs.Y)
			Dim a As Object
			If chkSelect.CheckState = 0 Then
				a = DeleteParent((grdFunction.Columns(3).Value), 0)
			Else
				a = UpdateChildSelected((grdFunction.Columns(3).Value), 0)
			End If
			Adodc1.Refresh()
			Adodc1.Recordset.AbsolutePosition = CShort(PadcodeSelect)
			'grdFunction.CtlRefresh()
			grdFunction.Refresh()
			FrameCheck.Top = VB6.TwipsToPixelsY(grdFunction.RowTop(grdFunction.Row) + 20)
			chkSelect.CheckState = IIf(grdFunction.Columns(1).Value = -1, 1, 0)
			'grdFunction.Row = OldRow
		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Sub OpenFile()
			Dim RecMenuPad As ADODB.Recordset
			Dim mCaption As String
			Dim tmp As String
			DBconn.Execute("DELETE FROM GroupFunctionTemp WHERE SoMay=" & MAYSO)
			RecMenuPad = DBconn.Execute("Select * from MenuPad order by Padcode ")
			Do While Not RecMenuPad.EOF
				If InStr(1, RecMenuPad.Fields("Padcode").Value, "iv", 0) > 0 Then
					tmp = RecMenuPad.Fields("FullPadName").Value
					Select Case Len(RecMenuPad.Fields("FullPadName").Value)
						Case 2
							mCaption = RecMenuPad.Fields("Caption").Value
						Case 6
							mCaption = "      " & RecMenuPad.Fields("Caption").Value
						Case 11
							mCaption = "            " & RecMenuPad.Fields("Caption").Value
						Case 17
							mCaption = "                  " & RecMenuPad.Fields("Caption").Value
					End Select
				Else
					Select Case Len(RecMenuPad.Fields("FullPadName").Value)
						Case 1
							mCaption = RecMenuPad.Fields("Caption").Value
						Case 4
							mCaption = "      " & RecMenuPad.Fields("Caption").Value
						Case 8
							mCaption = "            " & RecMenuPad.Fields("Caption").Value
						Case 13
							mCaption = "                  " & RecMenuPad.Fields("Caption").Value
					End Select
				End If
				DBconn.Execute("INSERT INTO GroupFunctionTemp (Padcode,Caption,Selected,SoMay)" & " SELECT Padcode,'" & mCaption & "',0," & MAYSO & " FROM MenuPad " & " Where padcode='" & RecMenuPad.Fields("Padcode").Value & "'")
				RecMenuPad.MoveNext()
			Loop


			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc1.RecordSource = "SELECT GroupFunctionTemp.Padcode, GroupFunctionTemp.Caption, " & " GroupFunctionTemp.Selected, GroupFunctionTemp.SoMay FROM GroupFunctionTemp INNER JOIN " & " MenuPad ON  GroupFunctionTemp.Padcode = MenuPad.PadCode WHERE Somay=" & MAYSO & " Order by GroupFunctionTemp.Padcode "
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc2.RecordSource = "Select * from UserGroup"
			Adodc2.Refresh()
		End Sub
		Sub UpdateSelected()
			Dim RecGroupFunction As ADODB.Recordset
			DBconn.Execute("UPDATE GroupFunctionTemp SET Selected=0 where  Somay=" & MAYSO)
			RecGroupFunction = DBconn.Execute("Select * from GroupFunction WHERE UserGroup='" & cboUserGroup.BoundText & "'")
			With RecGroupFunction
				Do While Not .EOF
					DBconn.Execute("UPDATE GroupFunctionTemp SET Selected=1 where Padcode='" & .Fields("Padcode").Value & "'" & " AND Somay=" & MAYSO)
					.MoveNext()
				Loop
				.Close()
			End With

		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			Dim a As Object
			a = UpdateChildSelected("s1", 0)
			a = UpdateChildSelected("s3", 0)
			a = UpdateChildSelected("s5", 0)

			DBconn.Execute("Delete from  GroupFunction where usergroup='" & cboUserGroup.BoundText & "'")
			DBconn.Execute("Insert Into GroupFunction(UserGroup,PadCode) " & " Select '" & cboUserGroup.BoundText & "',Padcode FROM GroupFunctionTemp " & " WHERE Selected=1 and somay=" & MAYSO)
			MsgBox("Scuccessfull!")
		End Sub

		Private Sub frmProgramRight_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

			OpenFile()
			VB6_AddADODataBinding()
			cboUserGroup.RowSource = ADOBind_Adodc2.DataSource
			grdFunction.DataSource = ADOBind_Adodc1.DataSource
			Dim myDataCaptions As String() = {"Selected", "Pad Name", "Pad code"}
			Dim myDataFields As String() = {"Selected", "Caption", "Padcode"}
			setDataGridView(myDataCaptions, myDataFields, Me.grdFunction)

		End Sub

		Private Sub grdFunction_RowColChange(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataGridLib.DDataGridEvents_RowColChangeEvent) Handles grdFunction.RowColChange
			'MsgBox("Row,Col=" & grdFunction.Row)
			FrameCheck.Visible = True
			OldRow = grdFunction.Row
			PadcodeSelect = Adodc1.Recordset.Bookmark
			'   FrameCheck.Top = VB6.TwipsToPixelsY(grdFunction.RowTop(grdFunction.Row) + 20)
			FrameCheck.Top = grdFunction.RowTop(grdFunction.Row) '+ 20
			chkSelect.CheckState = IIf(grdFunction.Columns(1).Value = -1, 1, 0)
		End Sub
		Function UpdateChildSelected(ByRef PadSelect As Object, ByRef UpdateType As Object) As Object
			Dim a As Object
			Dim RecGroupFunction, RecCount As Object
			Dim recChild As ADODB.Recordset
			Dim ParentCode As Object
			Dim FullPad As String
			Dim CountSameParent As Short
			RecGroupFunction = DBconn.Execute("SELECT " & " GroupFunctionTemp.Padcode, GroupFunctionTemp.Selected," & " MenuPad.ParentPadCode , MenuPad.FullPadName " & " FROM GroupFunctionTemp INNER JOIN    MenuPad ON    GroupFunctionTemp.Padcode = MenuPad.PadCode" & " WHERE somay=" & MAYSO & "  and GroupFunctionTemp.PadCode='" & PadSelect & "'")
			ParentCode = RecGroupFunction!ParentPadCode
			FullPad = RecGroupFunction!FullPadName

			'Select all Pad  hold Fullpad string (all child pad code of PadSelect)
			If UpdateType = 0 Then
				recChild = DBconn.Execute("SELECT " & " GroupFunctionTemp.Padcode, GroupFunctionTemp.Selected," & " MenuPad.ParentPadCode , MenuPad.FullPadName " & " FROM GroupFunctionTemp INNER JOIN    MenuPad ON    GroupFunctionTemp.Padcode = MenuPad.PadCode" & " WHERE somay=" & MAYSO & "  ORDER BY  FullPadName ")
				If Not recChild.EOF Then
					Do Until recChild.Fields("FullPadName").Value = FullPad
						recChild.MoveNext()
						If recChild.EOF Then Exit Do
					Loop
				End If
				Do While Not recChild.EOF
					If InStr(1, recChild.Fields("FullPadName").Value, FullPad, 0) > 0 Then
						DBconn.Execute("UPDate GroupFunctionTemp SET Selected=1 Where Padcode='" & recChild.Fields("Padcode").Value & "'")
					Else
						Exit Do
					End If
					recChild.MoveNext()
				Loop
			End If
			DBconn.Execute("UPDate GroupFunctionTemp SET Selected=1 Where Padcode='" & PadSelect & "'")
			If ParentCode = "" Then Exit Function
			a = UpdateChildSelected(ParentCode, 1)

		End Function

		Private Sub grdFunction_ScrollEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataGridLib.DDataGridEvents_ScrollEvent) Handles grdFunction.ScrollEvent
			FrameCheck.Visible = False
		End Sub


	End Class
End Namespace